* variables preparation for estimation, and further sampling 

 version 13
set more off
capture log close

local gruppo = 1
log  using  .../A_7_`gruppo'_03Sep2020.smcl, replace

use .../left_cens_spell_10sample_10never_type_`gruppo'_Sept2020_hmle.dta, clear

gen missconv=convexity_naer==.
bys id: egen dropmissconv=max(missconv)
drop if dropmissconv==1


gen everself1=lm_status==1
bysort id: egen everself1sum=max(everself1)

set more off
sort id year
capture drop _merge
preserve
tempfile tmp
set seed 122
bysort id: keep if _n == 1
sample 20 
sort id year
save `tmp'
restore
merge m:1 id using `tmp'
keep if _merge == 3
drop _merge 


gen pweight = 1
replace pweight = 10  if everself1sum == 0 
	
tab a_seq if sedum==1 & lcens_spell==0, gen(se_)
tab a_seq if sedum==0 & lcens_spell==0, gen(e_)

tab a_seq if sedum==1 & lcens_spell==1, gen(self_)
tab a_seq if sedum==0 & lcens_spell==1, gen(elf_)

* the above will create duration dummies se_dum1, se_dum2,  etc and e_dum1, e_dum2, etc...
* these are to pick up the duration dependence.  i.e. shape of the hazard
* now create the covariates. these have to be interacted with the sedum.

tab year if sedum==1 & lcens_spell==0, gen(se_time_)    /* dummies going from 1-18 */
tab year if sedum==0 & lcens_spell==0, gen(e_time_)    /* dummies going from 1-18 */
                    
tab year if sedum==1 & lcens_spell==1, gen(self_time_)    /* dummies going from 1-18 */
tab year if sedum==0 & lcens_spell==1, gen(elf_time_)    /* dummies going from 1-18 */


tab group_lev_edu* if sedum==1 & lcens_spell==0, gen(se_edu_)    /* goes from 1 to 3 */
tab group_lev_edu* if sedum==0 & lcens_spell==0, gen(e_edu_)    /* goes from 1 to 3 */
                              
tab group_lev_edu* if sedum==1 & lcens_spell==1, gen(self_edu_)    /* goes from 1 to 3 */
tab group_lev_edu* if sedum==0 & lcens_spell==1, gen(elf_edu_)    /* goes from 1 to 3 */

* substitute regions with 5  regions based on local labour market, ref comment
drop region 
g arbregion = 0
replace muni = 391  if muni == 301
replace muni = 0716 if muni == 718
replace muni = 1160 if muni == 1154
replace muni = 1160 if muni == 1159
replace muni = 1160 if muni == 1214
replace muni = 1505 if muni == 1503
replace muni = 1505 if muni == 1556
replace muni = 1576 if muni == 1569
replace muni = 1576 if muni == 1572
replace muni = 1756 if muni == 1729
replace muni = 1756 if muni == 1723
replace muni = 1804 if muni == 1842
replace muni = 1903 if muni == 1901 | muni == 1915


replace arbregion = 12	if  muni == 104 |   muni ==119 |   muni ==121 |   muni ==122 |   muni ==123 |   muni ==124 |   muni ==125 |   muni ==127 |   muni ==135 |   muni ==136 |   muni ==137 |   muni ==138 |   muni ==211 |   muni ==213 |   muni ==214 |   muni ==215 |   muni ==216 |   muni ==217 |   muni ==219 |   muni ==220 |   muni ==221 |   muni ==226 |   muni ==227 |   muni ==228 |   muni ==229 |   muni ==230 |   muni ==231 |   muni ==233 |   muni ==234 |   muni ==235 |   muni ==236 |   muni ==237 |   muni ==238 |   muni ==239 |   muni ==391 |   muni ==532 |   muni ==533 |   muni ==534 |   muni ==602 |   muni ==605 |   muni ==612 |   muni ==621 |   muni ==622 |   muni ==623 |   muni ==624 |   muni ==625 |   muni ==626 |   muni ==627 |   muni ==628 |   muni ==711 |   muni ==713 
replace arbregion = 11	if  muni == 101 |   muni ==118 |   muni ==106 |   muni ==105 |   muni ==111 |   muni ==128 
replace arbregion = 13	if  muni == 704 |   muni ==701 |   muni ==716 |   muni ==719 |   muni ==720 |   muni ==722 |   muni ==723 |   muni ==702 |   muni ==714 |   muni ==706 |   muni ==709 |   muni ==728
replace arbregion = 14	if  muni == 604 |   muni ==631 |   muni ==632 |   muni ==633 
replace arbregion = 15	if  muni == 617 |   muni ==615 |   muni ==616 |   muni ==618 |   muni ==619 |   muni ==620  
replace arbregion = 21	if  muni == 542 |   muni ==540 |   muni ==541 |   muni ==543 |   muni ==544 |   muni ==545  
replace arbregion = 22	if  muni == 516 |   muni ==519 |   muni ==520 |   muni ==517 |   muni ==511 |   muni ==512 |   muni ==513 |   muni ==514 |   muni ==515  
replace arbregion = 23	if  muni == 501 |   muni ==521 |   muni ==522 
replace arbregion = 24	if  muni == 502 |   muni ==528 |   muni ==529 |   muni ==536 |   muni ==538 
replace arbregion = 25	if  muni == 403 |   muni ==412 |   muni ==415 |   muni ==417 
replace arbregion = 26	if  muni == 402 |   muni ==418 |   muni ==419 |   muni ==420 |   muni ==423 |   muni ==425 
replace arbregion = 27	if  muni == 427 |   muni ==426 |   muni ==428 |   muni ==429 |   muni ==430 |   muni ==434 
replace arbregion = 28	if  muni == 437 |   muni ==432 |   muni ==436 |   muni ==438 |   muni ==439 |   muni ==441 |   muni ==1640 |   muni ==1644 
replace arbregion = 31	if  muni == 828 |   muni ==829 |   muni ==830 |   muni ==831 |   muni ==833 |   muni ==834 |   muni ==826 
replace arbregion = 32	if  muni == 807 |   muni ==821 |   muni ==822 |   muni ==827 
replace arbregion = 33	if  muni == 806 |   muni ==805 |   muni ==811 |   muni ==814 |   muni ==819 |   muni ==815 |   muni ==817 
replace arbregion = 34	if  muni == 906 |   muni ==904 |   muni ==912 |   muni ==914 |   muni ==919 |   muni ==929 |   muni ==901 |   muni ==911 
replace arbregion = 35	if  muni == 1001 |   muni ==1014 |   muni ==1017 |   muni ==1018 |   muni ==1002 |   muni ==1021 |   muni ==1026 |   muni ==1027 |   muni ==1029 |   muni ==926 |   muni ==928 |   muni ==937 |   muni ==935 |   muni ==938 |   muni ==940 |   muni ==941 
replace arbregion = 36	if  muni == 1003 |   muni ==1032 |   muni ==1034 |   muni ==1004 |   muni ==1037 |   muni ==1046 
replace arbregion = 41	if  muni == 1103 |   muni ==1102 |   muni ==1122 |   muni ==1124 |   muni ==1127 |   muni ==1129 |   muni ==1130 |   muni ==1133 |   muni ==1141 |   muni ==1142 |   muni ==1144 |   muni ==1121 |   muni ==1119 |   muni ==1120 |   muni ==1101 |   muni ==1111 |   muni ==1112 |   muni ==1114 
replace arbregion = 42	if  muni == 1106 |   muni ==1134 |   muni ==1135 |   muni ==1145 |   muni ==1146 |   muni ==1149 |   muni ==1151 |   muni ==1211 |   muni ==1216 |   muni ==1160 
replace arbregion = 43	if  muni == 1221 |   muni ==1219 |   muni ==1222 |   muni ==1223 |   muni ==1224 
replace arbregion = 51	if  muni == 1432 |   muni ==1413 |   muni ==1428 |   muni ==1429 |   muni ==1430 |   muni ==1431 |   muni ==1433 |   muni ==1401 |   muni ==1438 |   muni ==1416 |   muni ==1418 |   muni ==1412 
replace arbregion = 52	if  muni == 1420 |   muni ==1424 |   muni ==1417 |   muni ==1419 |   muni ==1421 |   muni ==1422 |   muni ==1426 
replace arbregion = 53	if  muni == 1439 |   muni ==1443 |   muni ==1445 |   muni ==1449 |   muni ==1441 |   muni ==1444 
replace arbregion = 54	if  muni == 1516 |   muni ==1511 |   muni ==1514 |   muni ==1515 |   muni ==1517 |   muni ==1520 |   muni ==1519 
replace arbregion = 55	if  muni == 1504 |   muni ==1523 |   muni ==1524 |   muni ==1525 |   muni ==1526 |   muni ==1528 |   muni ==1529 |   muni ==1531 |   muni ==1532 |   muni ==1534 |   muni ==1546 
replace arbregion = 56	if  muni == 1502 |   muni ==1535 |   muni ==1539 |   muni ==1543 |   muni ==1545 |   muni ==1547 |   muni ==1548 |   muni ==1551 |   muni ==1557 
replace arbregion = 57	if  muni == 1563 |   muni ==1560 |   muni ==1566 |   muni ==1567 |   muni ==1571 
replace arbregion = 58	if  muni == 1505 |   muni ==1554 |   muni ==1576 |   muni ==1573 
replace arbregion = 44	if  muni == 1201 |   muni ==1238 |   muni ==1241 |   muni ==1242 |   muni ==1243 |   muni ==1244 |   muni ==1245 |   muni ==1246 |   muni ==1247 |   muni ==1251 |   muni ==1252 |   muni ==1253 |   muni ==1256 |   muni ==1259 |   muni ==1260 |   muni ==1263 |   muni ==1264 |   muni ==1265 |   muni ==1266 |   muni ==1228 |   muni ==1227 |   muni ==1231 |   muni ==1232 |   muni ==1235 |   muni ==1233 |   muni ==1234 |   muni ==1411 
replace arbregion = 61	if  muni == 1601 | muni == 1624| muni ==1648| muni ==1653| muni ==1657| muni ==1662| muni ==1663| muni ==1664| muni ==1665| muni ==1620| muni ==1617| muni ==1621| muni ==1627| muni ==1630| muni ==1632| muni ==1633| muni ==1634| muni ==1635| muni ==1638| muni ==1612| muni ==1613| muni ==1622| muni ==1636| muni ==1714| muni ==1711| muni ==1718
replace arbregion = 62	if  muni == 1702 | muni == 1724 | muni == 1725 | muni == 1756 | muni == 1736 | muni == 1719 | muni == 1721 | muni == 1717 
replace arbregion = 63	if  muni == 1703 | muni == 1743 | muni == 1744 | muni == 1748 | muni == 1749 | muni == 1742 | muni == 1738 | muni == 1739 | muni == 1740 | muni == 1750 | muni == 1751 | muni == 1755 
replace arbregion = 64	if  muni == 1820 | muni == 1818 | muni == 1822 | muni == 1827 | muni == 1834 | muni == 1835 | muni == 1813 | muni == 1811 | muni == 1812 | muni == 1815 | muni == 1816 
replace arbregion = 65  if  muni == 1824 | muni == 1825 | muni == 1826 | muni == 1833 | muni == 1828 | muni == 1832 
replace arbregion = 71  if  muni == 1804 | muni == 1836 | muni == 1837 | muni == 1838 | muni == 1839 | muni == 1840 | muni == 1841 | muni == 1845 | muni == 1848 | muni == 1849 
replace arbregion = 72  if  muni == 1805 | muni == 1850 | muni == 1851 | muni == 1852 | muni == 1853 | muni == 1854 | muni == 1919 
replace arbregion = 73  if  muni == 1870 | muni == 1866 | muni == 1867 | muni == 1868 | muni == 1871 
replace arbregion = 74	if  muni == 1865 | muni == 1856 | muni == 1857 | muni == 1859 | muni == 1860 | muni == 1874 
replace arbregion = 75	if  muni == 1903 | muni == 1911 | muni == 1913 | muni == 1917 
replace arbregion = 76	if  muni == 1924 | muni == 1920 | muni == 1922 | muni == 1923 | muni == 1931 | muni == 1925 | muni == 1926 | muni == 1927 | muni == 1928 | muni == 1929 
replace arbregion = 77	if  muni == 1902 | muni == 1933 | muni == 1936 | muni == 1938 | muni == 1939 | muni == 1942 | muni == 1941 | muni == 1943 | muni == 1940 
replace arbregion = 81  if  muni == 2012 | muni == 2011 | muni == 2014 | muni == 2015 
replace arbregion = 82  if  muni == 2004 | muni == 2017 | muni == 2018 | muni == 2019 | muni == 2020 | muni == 2021 | muni == 2022 | muni == 2023 
replace arbregion = 83  if  muni == 2003 | muni == 2002 | muni == 2024 | muni == 2025 | muni == 2027 | muni == 2028 | muni == 2030

label var arbregion "labor market region from M.Bhuller (2009)"





gen region=0
replace region=1 if arbregion>=11 & arbregion<30
replace region=2 if arbregion>=30 & arbregion<40
replace region=3 if arbregion>=41 & arbregion<60
replace region=4 if arbregion>=61 & arbregion<70
replace region=5 if arbregion>=71 & arbregion<90
drop arbregion
quietly tab region 	if sedum==1 & lcens_spell==0, gen(se_reg_) 
quietly tab region      if sedum==0 & lcens_spell==0, gen(e_reg_)  
                                   
quietly tab region 	if sedum==1 & lcens_spell==1, gen(self_reg_) 
quietly tab region      if sedum==0 & lcens_spell==1, gen(elf_reg_)  

quietly tab field_educ 	if sedum==1 & lcens_spell==0, gen(se_field_) 
quietly tab field_educ  if sedum==0 & lcens_spell==0, gen(e_field_)  
                                   
quietly tab field_educ 	if sedum==1 & lcens_spell==1, gen(self_field_) 
quietly tab field_educ  if sedum==0 & lcens_spell==1, gen(elf_field_)  

quietly tab female 	if sedum==1 & lcens_spell==0, gen(se_female_) 
quietly tab female 	if sedum==0 & lcens_spell==0, gen(e_female_)
                                   
quietly tab female 	if sedum==1 & lcens_spell==1, gen(self_female_) 
quietly tab female 	if sedum==0 & lcens_spell==1, gen(elf_female_) 

bys id a_spell: egen ageb=min(age)

gen se_ageb=ageb*sedum*(1-lcens_spell)
gen e_ageb=ageb*(1-sedum)*(1-lcens_spell)
gen self_ageb=ageb*sedum*(lcens_spell)
gen elf_ageb=ageb*(1-sedum)*(lcens_spell)

* the above will create variables interacted with the SE and E spell dummies.
gen t_se=a_seq*sedum*(1-lcens_spell)  //duration dependance
gen t_e=a_seq*(1-sedum)*(1-lcens_spell)  //duration dependance
gen t_self=a_seq*sedum*(lcens_spell)  //duration dependance
gen t_elf=a_seq*(1-sedum)*(lcens_spell)  //duration dependance

gen lnt_se=ln(a_seq)*sedum*(1-lcens_spell)   //duration dependance
gen lnt_e=ln(a_seq)*(1-sedum)*(1-lcens_spell)  //duration dependance
gen lnt_self=ln(a_seq)*sedum*(lcens_spell)   //duration dependance
gen lnt_elf=ln(a_seq)*(1-sedum)*(lcens_spell)  //duration dependance

gen year_g1=(year>=1993 & year<1999)
gen year_g2=(year>=1999 & year<2003)
gen year_g3=(year>=2003 & year<2008)
gen year_g4=(year>=2008 & year<=2011)


gen se_yr_1=year_g1*sedum*(1-lcens_spell)
gen se_yr_2=year_g2*sedum*(1-lcens_spell)
gen se_yr_3=year_g3*sedum*(1-lcens_spell)
gen se_yr_4=year_g4*sedum*(1-lcens_spell)

gen e_yr_1=year_g1*(1-sedum)*(1-lcens_spell)
gen e_yr_2=year_g2*(1-sedum)*(1-lcens_spell)
gen e_yr_3=year_g3*(1-sedum)*(1-lcens_spell)
gen e_yr_4=year_g4*(1-sedum)*(1-lcens_spell)

gen self_yr_1=year_g1*sedum*(lcens_spell)
gen self_yr_2=year_g2*sedum*(lcens_spell)
gen self_yr_3=year_g3*sedum*(lcens_spell)
gen self_yr_4=year_g4*sedum*(lcens_spell)

gen elf_yr_1=year_g1*(1-sedum)*(lcens_spell)
gen elf_yr_2=year_g2*(1-sedum)*(lcens_spell)
gen elf_yr_3=year_g3*(1-sedum)*(lcens_spell)
gen elf_yr_4=year_g4*(1-sedum)*(lcens_spell)



gen t2_se=(a_seq^2)*sedum*(1-lcens_spell)
gen t2_e=(a_seq^2)*(1-sedum)*(1-lcens_spell)

gen t3_se=(a_seq^3)*sedum*(1-lcens_spell)
gen t3_e=(a_seq^3)*(1-sedum)*(1-lcens_spell)

gen convexity_naer_100 =  convexity_naer* 100

gen se_convexity=convexity_naer_100*sedum*(1-lcens_spell)
gen e_convexity=convexity_naer_100*(1-sedum)*(1-lcens_spell)

gen se_wealth =net_wealth*sedum*(1-lcens_spell)
gen e_wealth=net_wealth*(1-sedum)*(1-lcens_spell)

gen se_netincdiff =netincdiff*sedum*(1-lcens_spell)
gen e_netincdiff=netincdiff*(1-sedum)*(1-lcens_spell)

gen se_netnae =ln(netnae)*sedum*(1-lcens_spell)
gen e_netnae=ln(netnae)*(1-sedum)*(1-lcens_spell)

gen se_netwage =ln(netwage)*sedum*(1-lcens_spell)
gen e_netwage=ln(netwage)*(1-sedum)*(1-lcens_spell)

gen t2_self=(a_seq^2)*sedum*(lcens_spell)
gen t2_elf=(a_seq^2)*(1-sedum)*(lcens_spell)

gen t3_self=(a_seq^3)*sedum*(lcens_spell)
gen t3_elf=(a_seq^3)*(1-sedum)*(lcens_spell)

gen self_convexity=convexity_naer_100*sedum*(lcens_spell)
gen elf_convexity=convexity_naer_100*(1-sedum)*(lcens_spell)

gen self_wealth =net_wealth*sedum*(lcens_spell)
gen elf_wealth=net_wealth*(1-sedum)*(lcens_spell)

gen self_netincdiff =netincdiff*sedum*(lcens_spell)
gen elf_netincdiff=netincdiff*(1-sedum)*(lcens_spell)

gen self_netnae =ln(netnae)*sedum*(lcens_spell)
gen elf_netnae=ln(netnae)*(1-sedum)*(lcens_spell)

gen self_netwage =ln(netwage)*sedum*(lcens_spell)
gen elf_netwage=ln(netwage)*(1-sedum)*(lcens_spell)

*replacing missing values 
*local varall "se_edu_1-se_edu_3 e_edu_1-e_edu_3 se_1-se_18  e_1-e_17"
local varall "se_* e_*  self_* elf_*"
describe `varall'
foreach x of varlist `varall'   {
	replace `x'=0 if `x'==.
}

save .../sample_estimation_`gruppo'_03Sept2020.dta, replace

	
//working	




